﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class PageDAL<T> where T:class
    {
        
        public bool Add(T model)
        {
            Type type = model.GetType();
            //获取所有属性
            PropertyInfo[] propertys = type.GetProperties();
            string strFields = "";
            string strvalues = "";
            for (int i = 1; i < propertys.Length; i++)
            {
                strFields += propertys[i].Name + ",";
                strvalues += "'" + propertys[i].GetValue(model) + "',";
            }
            strFields = strFields.Substring(0, strFields.Length - 1);
            strvalues = strvalues.Substring(0, strvalues.Length - 1);
            string tableName = type.Name.Substring(0, type.Name.Length - 5);
            string sql = $"insert {tableName}({strFields}) values({strvalues})";
            return new DBHelper().Zsg(sql);

        }
        public DataTable FY(string name,int pageIndex = 1)
        {
            string sql = $"select top 5 * from {name} where ID not in(select top {5 * (pageIndex - 1)} id from {name})";
            return new DBHelper().GetDataTable(sql);
        }
        public DataTable LJ(string Onename, string Twoname)
        {
            string sql = $"Select a.* , b.Name from {Onename} a,{Twoname} b where b.id = a.{Twoname}id";
            return new DBHelper().GetDataTable(sql);
        }
        public DataTable SelectID(string TableName, int ID)
        {
            string sql = $"select * from {TableName} where ID={ID}";
            return new DBHelper().GetDataTable(sql);
        }
        public DataTable GetDataTableByPage(string cpName, PageModel pageModel, ref int TotalRows)
        {
            return new DBHelper().GetDataTableByPage(cpName, pageModel, ref TotalRows);
        }
        public DataTable Select(string TableName)
        {
            string sql = $"select * from {TableName}";
            return new DBHelper().GetDataTable(sql);
        }
        public DataTable SelectNo(string TableName,string mz)
        {
            string sql = $"select * from {TableName} where Name like '%{mz}%'";
            return new DBHelper().GetDataTable(sql);
        }
       
        public bool Del(string Name, int id)
        {
            string sql = $"delete from {Name} where id='{id}'";
            return new DBHelper().Zsg(sql);
        }
        public bool Gai(T model, int pid)
        {
            Type type = model.GetType();
            PropertyInfo[] infos = type.GetProperties();
            string zong = "";
            for (int i = 1; i < infos.Length; i++)
            {
                zong += infos[i].Name + "='" + infos[i].GetValue(model) + "',";
            }
            zong = zong.Substring(0, zong.Length - 1);
            string tableName = type.Name.Substring(0, type.Name.Length - 5);
            string sql = $"update {tableName} set {zong} where id={pid}";
            return new DBHelper().Zsg(sql);
        }
        public DataTable SelectLJ(string TableOne, string TableTwo, string mz)
        {
            string sql = $"select * from {TableOne} a,{TableTwo} b where a.EmployeeName like '%{mz}%' and b.ID=a.{TableTwo}ID";
            return new DBHelper().GetDataTable(sql);
        }

        public DataTable S(string TableOne, string TableTwo, int pageIndex = 1)
        {
            string sql = $"select top 5 * from {TableOne} a,{TableTwo} b where a.ID not in(select top {5 * (pageIndex - 1)}  id from {TableOne}) and b.ID=a.{TableTwo}ID";
            return new DBHelper().GetDataTable(sql);
        }
    }
}
